package net.alche.sdd.servlet.imp;

import java.util.EnumSet;
import java.util.Set;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.alche.al.util.Far;

public class SddHttpServerTomcatInitializer implements ServletContainerInitializer {
  public static final Logger logger = LoggerFactory.getLogger(SddHttpServerTomcatInitializer.class);
  public static SddHttpServerTomcat sddServer = null;

  @Override
  public void onStartup(Set<Class<?>> c, ServletContext servletContext) throws ServletException {
    Far.log("on TOMCAT Startup!");
    SddHttpServerTomcat.initLogger();
    ServletRegistration.Dynamic servlet = servletContext.addServlet(SddHttpServerTomcatServlet.class.getSimpleName(),
        SddHttpServerTomcatServlet.class);
    // 添加地址映射
    servlet.addMapping("/");
    FilterRegistration.Dynamic filter = servletContext.addFilter(SddHttpServerTomcatFilter.class.getSimpleName(),
        SddHttpServerTomcatFilter.class);
    EnumSet<DispatcherType> dispatcherTypes = EnumSet.allOf(DispatcherType.class);
    dispatcherTypes.add(DispatcherType.REQUEST);
    dispatcherTypes.add(DispatcherType.FORWARD);
    filter.addMappingForUrlPatterns(dispatcherTypes, true, "/");
    sddServer = new SddHttpServerTomcat(true);
    if (sddServer.appBiz.hasKey("servlet.sessionTimeout"))
      servletContext.setSessionTimeout(sddServer.appBiz.getInt("servlet.sessionTimeout"));
  }
}
